/*
ID: quxiao82
LANG: C++
PROG: spin
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;

const int NUM = 5;

struct Internal
{
     int from;
     int len;
};

int speed[NUM];
vector<Internal> wedge[NUM];
int originAngle[NUM];
int light[360];

void Input ()
{
     int i, j, n;
     Internal tmp;
     for (i = 0; i < NUM; i ++)
     {
          scanf("%d%d", &speed[i], &n);
          for (j = 0; j < n; j ++)
          {
               scanf("%d%d", &tmp.from, &tmp.len);
               wedge[i].push_back(tmp);
               if ( j == 0 )
                    originAngle[i] = tmp.from;
          }
     }
}

bool IsOrigin ()
{
     int i;
     for (i = 0; i < NUM; i ++)
     {
          if ( originAngle[i] != wedge[i][0].from )
          {
               return false;
          }
     }
     return true;
}

void TestOut ()
{
     int i, j;
     for (i = 0; i < NUM; i ++)
     {
          for (j = 0; j < wedge[i].size(); j ++)
               printf("(%d %d) ", wedge[i][j].from, wedge[i][j].len);
          printf("\n");
     }
     printf("\n");
}

void SetLight(int from, int len)
{
     int i;
     for (i = 0; i <= len; i ++)
     {
          if ( from >= 360 )
               from -= 360;
          light[from++] ++;
     }
}

bool CheckLight()
{
     int i;
     for (i = 0; i < 360; i ++)
     {
          if ( light[i] == NUM )
               return true;
     }
     return false;
}

void OutputLight()
{
     int i;
     for (i = 0; i < 360; i ++)
          printf("%d ", light[i]);
     printf("\n");
}

void Solve ()
{
     int i, j, k;

     for (k = 0;;k ++)
     {
          memset(light, 0, sizeof(light));
          for (i = 0; i < NUM; i ++)
          {
               for (j = 0; j < wedge[i].size(); j ++)
               {
                    SetLight(wedge[i][j].from, wedge[i][j].len);
               }
          }
//           TestOut();
//           OutputLight();
//           system("pause");
          if ( CheckLight() )
          {
               printf("%d\n", k);
               return;
          }
          for (i = 0; i < NUM; i ++)
          {
               for (j = 0; j < wedge[i].size(); j ++)
               {
                    wedge[i][j].from += speed[i];
                    if ( wedge[i][j].from >= 360 )
                         wedge[i][j].from -= 360;
               }
          }
          if ( IsOrigin() )
               break;
     }
     printf("none\n");
}

int main ()
{
     freopen("spin.in", "r", stdin);
     freopen("spin.out", "w", stdout);

     Input ();
     Solve ();

     return 0;
}